﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HotelProject
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            HotelSearchQuery query = new HotelSearchQuery()
            {
                checkInDate = DateTime.Now.AddDays(-2),
                checkOutDate = DateTime.Now,
                hotelID = 1,
                minPrice = 200
            };

            hotelRoom[] rooms = HotelSearch.search(query);
            if (!IsPostBack)
            { 
                populateHotelsInDropDown(hotelSelection); 
            }
        }

        private void populateHotelsInDropDown(DropDownList list)
        {
            MySQLDatabaseConnector db = new MySQLDatabaseConnector();
            
            hotel[] hotels = db.getHotels();
            List<System.Web.UI.WebControls.ListItem> hotelsAsLitsItems = new List<System.Web.UI.WebControls.ListItem>();

            hotelsAsLitsItems.Add(new ListItem("All Hotels", "0"));
            foreach (hotel _hotel in hotels)
            {
                hotelsAsLitsItems.Add(new ListItem(_hotel.hotelName, _hotel.hotelID.ToString()));
            }

            list.DataSource = hotelsAsLitsItems;
            list.DataBind();
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            DateTime checkInDate, checkOutDate;
            DateTime.TryParse(txtDate1.Text, out checkInDate);
            DateTime.TryParse(txtDate2.Text, out checkOutDate);

            HotelSearchQuery searchQuery = new HotelSearchQuery() 
            { 
                hotelID = int.Parse(hotelSelection.SelectedValue),
                checkInDate = checkInDate,
                checkOutDate = checkOutDate
                
            };

            Session["searchQuery"] = searchQuery;
                      
            Response.Redirect("Results.aspx");

        }
    }
}
